class StudentEnrollment < ActiveRecord::Base
  belongs_to :student
  belongs_to :group
  belongs_to :grade
  belongs_to :enrollment_type

  validates_presence_of :student_id, :group_id, :grade_id, :enrollment_type_id
  validate :previous_enrollment

  protected
  def previous_enrollment
    found = this.count(:conditions => ["student_id = ? AND group_id = ? AND enrollment_type = ?",
        student_id, group_id, enrollment_type_id])
    errors.add(:group, 'El alumno ya ha sido inscrito a este grupo.') if found.size > 0
  end
end
